<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class UserController extends BaseController {
    
    public function _initialize() {
        parent::_initialize();
        if (!$this->user_id) {
            $this->error('请先登录');
        }
    }
    
    //获取当前登录用户信息
    public function getUserInfo(){
        $user_id = $this->user_id;
        $where['u.company_user_id'] = $user_id;
        $userInfo = M()->field("u.company_user_id as user_id,u.company_user_phone as phone,u.company_user_name as user_name,u.company_head_img as head_img,u.company_id,u.status,u.create_time")
                ->table("ot_company_user as u ")
                ->where($where)
                ->find();
        if ($userInfo['status'] == 1) {
            $this->error('您的账号已被禁用,请联系管理员');
            return;
        } else {
            !empty($userInfo['head_img']) && $userInfo['head_img'] = C('SITE_URL'). $userInfo['head_img'];
            $userInfo['company_info'] = M('Company')->field('company_name,status as company_status,refund_desc,total_amount')->where(array("company_id" => $userInfo['company_id']))->find();
        }
        $this->returnList($userInfo);
    }
    public function setUserInfo() {
        $user_id = I('user_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error('请传入user_id');
        }

        $face = I('company_head_img');
        $nickName = I('company_user_name');

        $data = [];
        if (!empty($face)) {
            $code = md5(time() + mt_rand(1, 9999));
            $s = file_put_contents('./Uploads/Picture/' . $code . '.png', base64_decode($face)); //返回的是字节数

            $face_url = '/Uploads/Picture/' . $code . '.png';

            $data['company_head_img'] = $face_url;
        }

        if (!empty($nickName)) {
            $data['company_user_name'] = $nickName;
        }

        if (empty($data)) {
            $this->error('没有要修改的数据');
        }

        $res = M("CompanyUser")->where(array('company_user_id' => $user_id))->save($data);

        if ($res) {
            $this->success('修改成功!');
        } else {
            $this->error('修改失败!');
        }
    }

    //获取企业子账号
    public function companyUser() {
        $user_id = I('user_id', 0, 'intval');

        $company_info = M('Company')->where(array("company_user_id" => $user_id))->find();

        if (empty($company_info)) {
            $this->success('没有获取到子账号!');
        } else {
            $user_list = M('CompanyUser')->field('company_user_id,company_user_phone,company_user_name')->where(array('company_id' => $company_info['company_id']))->select();

            $users = [];
            if (!empty($user_list)) {
                foreach ($user_list as $val) {
                    if ($val['company_user_id'] != $user_id) {
                        $users[] = $val;
                    }
                }
            }

            $this->success('获取成功', $users);
        }
    }

    //添加企业子账号
    public function addEditUser() {
        $user_id = I('user_id', 0, 'intval');

        $company_info = M('Company')->where(array("company_user_id" => $user_id))->find();

        if (empty($company_info)) {
            $this->success('你不是企业的管理员,不允许操作!');
        }

        $company_user_id = I('company_user_id', 0, 'intval');
        $company_user_name = I('company_user_name');
        $company_user_phone = I('company_user_phone');

        empty($company_user_name) && $this->error("请输入子账号姓名");
        empty($company_user_phone) && $this->error("请输入子账号手机号");

        $data['company_user_name'] = $company_user_name;
        $data['company_user_phone'] = $company_user_phone;

        $res = 0;
        if ($company_user_id == 0) {
            $user_info = M('CompanyUser')->where(array('company_user_phone' => $company_user_phone))->find();

            if (!empty($user_info)) {
                $this->error("该手机号已存在,请确认!");
            }

            $data['company_id'] = $company_info['company_id'];
            $res = M('CompanyUser')->add($data);
        } else {
            $res = M('CompanyUser')->where(array('company_user_id' => $company_user_id))->save($data);
        }

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //删除企业子账号
    public function delUser() {
        $user_id = I('user_id', 0, 'intval');

        $company_info = M('Company')->where(array("company_user_id" => $user_id))->find();

        if (empty($company_info)) {
            $this->success('你不是企业的管理员,不允许操作!');
        }

        $company_user_id = I('company_user_id', 0, 'intval');

        if ($company_user_id == 0) {
            $this->error("请确认要删除的子账号ID");
        }

        $res = M('CompanyUser')->where(array('company_user_id' => $company_user_id))->delete();

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //获取提箱地址
    public function getBoxAddress() {
        $user_id = I('user_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }
        $address_list = M('BoxAddress')->where(array('user_id' => $user_id, 'is_use' => 0, 'is_delete' => 0))->select();
        
        $this->success('获取成功', $address_list);
    }

    //新增修改提箱地址
    public function addEditBoxAddress() {
        $user_id = I('user_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        $box_address_id = I('box_address_id', 0, 'intval');
        $box_address_desc = I('box_address_desc');
        $box_linkman = I('box_linkman');
        $box_linkman_phone = I('box_linkman_phone');

        empty($box_address_desc) && $this->error("请输入拿箱地址");
        empty($box_linkman) && $this->error("请输入拿箱联系人姓名");
        empty($box_linkman_phone) && $this->error("请输入拿箱联系人电话");

        $is_use = I('is_use', 0, 'intval');

        $data['box_address_desc'] = trim($box_address_desc);
        $data['box_linkman'] = trim($box_linkman);
        $data['box_linkman_phone'] = trim($box_linkman_phone);
        $data['is_use'] = $is_use;

        $res = 0;
        if ($box_address_id == 0) {
            $data['user_id'] = $user_id;
            //判断是存在
            $whereE['box_linkman'] = trim($box_linkman);
            $whereE['box_linkman_phone'] = trim($box_linkman_phone);
            $whereE['box_address_desc'] = trim($box_address_desc);
            $addressInfoE = M('box_address')->where($whereE)->find();
            if($addressInfoE){
                $res = $addressInfoE['box_address_id'];
                $box_address_id = $addressInfoE['box_address_id'];
            }else{
                $box_address_id = M('BoxAddress')->add($data);
                $res = $box_address_id;
            }
        } else {
            $res = M('BoxAddress')->where(array('box_address_id' => $box_address_id, 'user_id' => $user_id))->save($data);
        }

        if ($res) {
            $this->success('操作成功!', ['box_address_id' => $box_address_id]);
        } else {
            $this->error();
        }
    }
    
    //提箱地址详情
    public function boxAddressInfo() {
        $user_id = $this->user_id;
        $box_address_id = I('box_address_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        if ($box_address_id == 0) {
            $this->error("请传入拿箱地址ID");
        }

        $address_info = M('BoxAddress')->where(array('box_address_id' => $box_address_id, 'user_id' => $user_id))->find();

        if ($address_info) {
            $this->success('操作成功!', $address_info);
        } else {
            $this->error('没有获取到该ID的详情');
        }
    }

    //删除提箱地址
    public function delBoxAddress() {
        $user_id = $this->user_id;
        $box_address_id = I('box_address_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        if ($box_address_id == 0) {
            $this->error("请传入拿箱地址ID");
        }

        $address_info = M('BoxAddress')->where(array('box_address_id' => $box_address_id, 'user_id' => $user_id))->save(['is_delete' => 1]);

        if ($address_info) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //获取消息列表
    public function getMsgList() {
        $user_id = I('user_id', 0, 'intval');
        $is_read = I('is_read', 0, 'intval');
        $planfrom = I('planfrom', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        $where['user_id'] = $user_id;
        $where['planfrom'] = $planfrom;

        if ($is_read != -1) {
            $where['is_read'] = $is_read;
        }
        
        $msg_list = M('Msg')->where($where)->select();

        $this->success('获取成功!', $msg_list);
    }

    //获取未读消息数
    public function getNotReadMsgNum() {
        $user_id = I('user_id', 0, 'intval');
        $is_read = 0;
        $planfrom = I('planfrom', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        $where['user_id'] = $user_id;
        $where['planfrom'] = $planfrom;

        if ($is_read != -1) {
            $where['is_read'] = $is_read;
        }


        $msg_list = M('Msg')->where($where)->count();
        $data['num'] = $msg_list;
        $this->success('获取成功!', $data);
    }

    //装箱点列表
    public function getShipAddress() {
        $user_id = $this->user_id;
        $province = $this->getParam('province');
        $city = $this->getParam('city');
        
        !empty($province) && $where['province'] = $province;
        !empty($city) && $where['city'] = $city;
        $where['sa.user_id'] = $user_id;
        $where['sa.is_del'] = 0;
        $list = M()->table('ot_shipment_address sa')
                ->field('sa.*,b.boxman_name as shipment_linkman,b.boxman_phone as shipment_linkman_phone,la.loadarea_name')
                ->join('LEFT JOIN ot_boxman AS b ON b.boxman_id = sa.boxman_id')
                ->join('LEFT JOIN ot_load_area AS la ON la.loadarea_id= sa.loadarea_id')
                ->where($where)
                ->order('sa.shipment_address_id desc')
                ->select();

        $this->success('获取成功!', $list);
    }

    //新增或编辑装箱点
    public function addEditShipAddress() {
        $user_id = $this->user_id;
        $shipment_address_id = I('shipment_address_id', 0, 'intval');
        $loadarea_id = $this->getParam('loadarea_id', 0, 'intval', TRUE);
        $province = $this->getParam('province', '');
        $city = $this->getParam('city', '');
        $shipment_linkman_phone = I('shipment_linkman_phone');
        $address_desc = I('address_desc');
        $shipment_linkman = I('shipment_linkman');
        $lon = $this->getParam('lon', '', 'trim', TRUE, '请定位经度');
        $lat = $this->getParam('lat', '', 'trim', TRUE, '请定位纬度');
        
        $address = I('address');
        if (empty($address)) {
            $this->error("请完善具体地址");
        }
        if (empty($shipment_linkman_phone)) {
            $this->error("请填写联系人电话");
        }
        $BoxmanModel =  new \Common\Model\BoxmanModel();
        $boxman_id = $BoxmanModel->updateOrderBoxman($shipment_linkman_phone, $shipment_linkman);
        
        $data['user_id'] = $user_id;
        $data['boxman_id'] = $boxman_id;
        $data['province'] = $province;
        $data['city'] = $city;
        $data['address'] = $address;
        $data['address_desc'] = $address_desc;
        if($lat != 0 && $lon != 0){
            $data['lon'] = $lon;
            $data['lat'] = $lat;
        }
        
        $data['loadarea_id'] = $loadarea_id;
        $res = 0;
        if ($shipment_address_id == 0) {
            $res = M('ShipmentAddress')->add($data);
            $shipment_address_id = $res;
        } else {
            $where['shipment_address_id'] = $shipment_address_id;
            $res = M('ShipmentAddress')->where($where)->save($data);
        }
        
        $this->returnList(['shipment_address_id'=>$shipment_address_id]);
    }

    //装箱点详情
    public function getShipAddressInfo() {
        $user_id = I('user_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        $shipment_address_id = I('shipment_address_id', 0, 'intval');

        if ($shipment_address_id == 0) {
            $this->error("请选择装箱点");
        }

        $where['sa.user_id'] = $user_id;
        $where['sa.shipment_address_id'] = $shipment_address_id;

        $data = M()->table('ot_shipment_address as sa')
                ->field('sa.shipment_address_id,sa.address,sa.address_desc,sa.shipment_linkman,sa.shipment_linkman_phone,sa.create_time,la.loadarea_name,la.loadarea_id')
                ->join('ot_load_area as la ON sa.loadarea_id = la.loadarea_id')
                ->where($where)
                ->find();
        
        $this->success('获取成功!', $data);
    }

    //删除装箱点
    public function delShipAddress() {
        $user_id = I('user_id', 0, 'intval');

        if ($user_id == 0) {
            $this->error("请传入user_id");
        }

        $shipment_address_id = I('shipment_address_id', 0, 'intval');

        if ($shipment_address_id == 0) {
            $this->error("请选择装箱点");
        }

        $where['user_id'] = $user_id;
        $where['shipment_address_id'] = $shipment_address_id;

        $res = M('ShipmentAddress')->where($where)->save(['is_del' => 1]);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }
    
    //收支明细
    public function walletFlow(){
        $user_id = $this->user_id;
        $userInfo = M()->table('ot_company_user cu')
                    ->field('c.company_id,c.total_amount')
                    ->join('ot_company as c USING(company_id)')
                    ->where(array('cu.company_user_id' => $user_id))
                    ->find();
        
        $walletChangeList = M()->table('ot_wallet_change wc')->field('wc.*,o.pick_no as box_no,o.pick_no')
                            ->join('LEFT JOIN ot_order as o ON o.order_id = wc.order_id')
                            ->order("wc.wallet_change_id DESC")
                            ->where(['wc.company_id'=>$userInfo['company_id']])
                            ->order('wc.wallet_change_id DESC')
                            ->select();
        
        $this->returnList($walletChangeList);
    }
    
    //获取最近拿箱地址
    public function recentlyBoxAddress(){
        $user_id = $this->user_id;
        $addressList = M('Order')->field('DISTINCT box_address_id')->where(['company_user_id'=>$user_id])->order('order_id DESC')->limit(5)->select();
        
        $returnList = [];
        if($addressList){
            $BoxAddress = M('box_address');
            foreach($addressList as $val){
                $boxinfo = $BoxAddress->where(array('box_address_id'=>$val['box_address_id']))->find();
                if(!empty($boxinfo)){
                    $returnList[] = $boxinfo;
                }
            }
        }
        $this->returnList($returnList);
    }
    
    //获取最近留言记录
    public function recentlyMessageList(){
        $user_id = $this->user_id;
        $messageList = M('Order')->field('DISTINCT message')->where(['company_user_id'=>$user_id])->order('order_id DESC')->limit(5)->select();
        $returnList = array();
        if($messageList){
            foreach($messageList as $val){
                $returnList[] = $val['message'];
            }
        }
        $this->returnList($returnList);
    }
    
    //我的司机
    public function driverList(){
        $user_id = $this->user_id;
        $driver_ids = M('Order')->where(['company_user_id'=>$user_id])->distinct(true)->getField('driver_id',TRUE);
        $driverList = [];
        if($driver_ids){
            $driverList = M()->table('ot_driver d')->field('d.*,m.fleet_name')
                    ->join('LEFT JOIN ot_member as m ON m.uid = d.fleet_id')
                    ->select();
        }
        $this->returnList($driverList);
    }
    
    //消息列表
    public function msglist(){
        $where['planfrom'] = 0;
        $where['user_id'] = $this->user_id;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $list = M('msg')->where($where)->order('msg_id DESC')->select();
        $this->returnList($list);
    }
    
    //未读消息
    public function msgNoReadcount(){
        $where['planfrom'] = 0;
        $where['user_id'] = $this->user_id;
        $where['is_read'] = 0;
        $data['msg_no_read_count'] = M('msg')->where($where)->count();
        $this->returnList($data);
    }
    
    //更新已读
    public function updateMsgRead(){
        $where['msg_id'] = $this->getParam('msg_id',0,'int+',TRUE);
        $where['planfrom'] = 0;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $this->success();
    }

}
